import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/provider/out_warehousing_provider.dart';
import 'package:xiongan_app_yy/provider/toreview_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class OutWarehousing extends StatefulWidget {
  @override
  _OutWarehousingState createState() => _OutWarehousingState();
}

class _OutWarehousingState extends State<OutWarehousing> {
  List<dynamic> _data = List();

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    Provider.of<OutWareHousingProvider>(context).getList();
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<OutWareHousingProvider>(context).getList();
    });
  }

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<OutWareHousingProvider>(context).listData;
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "出库作业",
          style: TextStyle(fontSize: 18),
        ),
        actions: <Widget>[
          new IconButton(
            // action button
            padding: EdgeInsets.only(),
            iconSize: 22,
            icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
            onPressed: () {
              Navigator.pushNamed(context, "/outWarehousingSearchPage");
            },
          )
        ],
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
        visibility: Provider.of<OutWareHousingProvider>(context).netStatus1,
        emptyVisibility:!Provider.of<OutWareHousingProvider>(context).netStatus1&&_data.length==0,
        builder: Column(
          children: <Widget>[
            Expanded(
                child: RefreshIndicator(
                    child: ListView.builder(
                        itemBuilder: (BuildContext context, int index) {
                          return OutWareHousingItem(_data[index], index,
                              (id, name) {
                            if (name.contains("拣选")) {
                              Provider.of<AutoPickingProvider>(context)
                                  .getDetailListData(orderid: id);
                              Navigator.pushNamed(
                                  context, '/autopicking_type',arguments: id);
                            } else if (name.contains("待复核")||name.contains("复核完成")||name.contains("已完成")) {
                              Provider.of<ToReviewProvider>(context)
                                  .getDetailListData(orderid: id);
                              Navigator.of(context)
                                  .pushNamed("/toreview_detail_list",arguments: _data[index]);
                            }/* else if (name.contains("待交接") ||name.contains("复核完成") || name == "已完成") {
                              Provider.of<OutHandoverDetailsInfoProvider>(context).getDetail(id);
                              Navigator.pushNamed(
                                  context, '/out_handover_details',
                                  arguments: _data[index]);
                            }*/
                          });
                        },
                        itemCount: _data.length),
                    onRefresh: _refresh)),
          ],
        ),
      ),
    );
  }
}

class OutWareHousingItem extends StatelessWidget {
  dynamic data;
  int index;
  Function(String id, String name) onTapFuns;

  OutWareHousingItem(this.data, this.index, this.onTapFuns);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      child: Container(
        color: Colors.white,
        margin: EdgeInsets.only(top: 6),
        padding: EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    RichText(
                      text: TextSpan(
                        text: "出库任务单号: ",
                        style:
                            TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                        children: <TextSpan>[
                          TextSpan(
                            text: "${data["orderno"]}",
                            style: TextStyle(
                                fontSize: 14.0, color: Color(0xFF00A5F1)),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6, bottom: 6),
                      child: _Text("出库类型: ${data["ordertypename"]}"),
                    ),
                    RichText(
                      text: TextSpan(
                        text: "货位分配状态: ",
                        style:
                            TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                        children: <TextSpan>[
                          TextSpan(
                            text: "${data["isallocation"] ? "已分配" : "待分配"}",
                            style: TextStyle(
                                fontSize: 14.0, color: Color(0xFF00A5F1)),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6),
                      child: _Text("需求部门：${data["demanddepartment"] ?? "暂无"}"),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6),
                      child: _Text("批次：${data["biddingbatchid"] ?? "暂无"}"),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6),
                      child: _Text("总价（元）：${data["amountCount"]}"),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6, bottom: 10),
                      child: _Text("预计出库时间：${data["estimatedeliverytime"]}"),
                    ),
                  ],
                )),
                Center(
                  child: Container(
                    width: 8,
                    height: 16,
                    child: Image.asset("assets/images/arrow.png"),
                  ),
                )
              ],
            ),
            Container(
              width: double.infinity,
              height: 1,
              color: Color(0xffeeeeee),
            ),
            Row(
              children: <Widget>[
                Expanded(
                    child: Padding(
                  padding: EdgeInsets.only(top: 10),
                  child: RichText(
                    text: TextSpan(
                      text: "状态： ",
                      style:
                          TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: "${data["orderstatetypename"]}",
                          style: TextStyle(
                              fontSize: 14.0, color: Color(0xFF00A5F1)),
                        ),
                      ],
                    ),
                  ),
                )),
                Padding(
                  padding: EdgeInsets.only(top: 10),
                  child: _Text("创建时间：${data["creationTime"]??""}"),
                )
              ],
            ),
          ],
        ),
      ),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(data["id"], data["orderstatetypename"]);
        }
      },
    );
  }
}

class _Text extends Text {
  TextStyle style;

  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}
